<?php
require './library/db_constants.php';
require './library/planets.php';
require './library/terrain.php';

// Call planets class for the createPlanet function.
$planets = new planets;
$terrain = new terrain;

if(!isset($_GET['id'])) {
    echo 'Invalid id. <a href="listPlanets.php">Go back.</a>';
    exit();
}

// Store planet info in variable
$planetInfo = $planets->planetInfo($_GET['id']);
        
if(isset($_POST['save'])) {
    
    $planets->editPlanet($_GET['id'], $_POST['planet_name'], 
            $_POST['planet_size'], $_POST['coordinates'], $_POST['terrain']);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>SWC Prospecting Manager || Edit Planets</title>
        <link rel="stylesheet" href="./library/css/editPlanets.css" />
    </head>
    <body>
        <h1>List/Edit Planets</h1>
        
        <hr />
        
        <div id="planetInfo">
            <fieldset>
                <legend>Edit Planet || <?php echo $planetInfo['name'];?></legend>
                
                <form action='<?php echo $_SERVER['PHP_SELF'];?>?id=<?php echo $_GET['id'];?>' method='post'>
                    <label for='planet_name'>Planet Name: </label>
                    <input type='text' name='planet_name' value='<?php echo $planetInfo['name'];?>'
                           size='30' maxlength='80'/>
                    <br />
                    <label for="planet_size">Planet Size: (no greater than 30) </label>
                    <input type="text" name="planet_size" value='<?php echo $planetInfo['size'];?>'
                           size='30' maxlength='80'/>
                    <br />
                    <div id="planet">
                        <?php $terrain->drawMap($planetInfo['id'], $planetInfo['size']);?>
                    </div>
                    <div id="editForm">
                        <label for="coordinates">Coordinates: </label>
                        <select name="coordinates">
                            <?php
                                for ($y = 0; $y <= $planetInfo['size']; $y++) {
                                    for ($x = 0; $x <= $planetInfo['size']; $x++) {
                                        echo '<option value="'.$x.', '.$y.'">'.$x.', '.$y.'</option>';
                                    }
                                }
                            ?>
                        </select>
                        <br />
                        <br />
                        <label for="terrain">Terrain: </label>
                        <select name="terrain" id="terrain">
                            <option value="0">Grassland</option>
                            <option value="1">Desert</option>
                            <option value="2">River</option>
                            <option value="3">Volcanic</option>
                            <option value="4">Crater</option>
                            <option value="5">Glacier</option>,
                            <option value="6">Ocean</option>
                            <option value="7">Swamp</option>
                            <option value="8">Cave</option>
                            <option value="9">Gas Giant</option>
                            <option value="10">Mountain</option>
                            <option value="11">Sun</option>
                            <option value="12">Black Hole</option>
                            <option value="13">Forest</option>
                            <option value="14">Jungle</option>
                            <option value="15">Rock</option>
                            <option value="16">None</option>
                        </select>
                        <br />
                        <br />
                        <input type="submit" name="save" value="Save Changes" />
                    </div>
                </form>
                
            </fieldset>
        </div>
        <footer>
            <a href="index.php">Home</a> || <a href="createPlanet.php">Create Planet</a> ||
            <a href="listPlanets.php">List/Edit Planets</a>
        </footer>
    </body>
</html>